#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

//先屏蔽2号信号，轮询10次解除屏蔽

void PrintPending(sigset_t &pending)
{
    cout<<"["<<getpid()<<"]:";
    for(int i = 31;i>=1;i--)
    {
        if(sigismember(&pending,i))
        {
            cout<<"1";
        }
        else
        {
            cout<<"0";
        }
    }
    cout<<endl;
}

void headler(int signo)//打印信号表
{
    cout<<"接收到信号："<<signo<<endl;
    cout<<"----------------------"<<endl;
    sigset_t pending;
    sigpending(&pending);
    PrintPending(pending);
    cout<<"----------------------"<<endl;
}

int main()
{
    //初始化
    sigset_t new_set,old_set;
    signal(2,headler);
    sigemptyset(&new_set);
    sigemptyset(&old_set);
    sigaddset(&new_set,2);
    
    //设置阻塞表
    sigprocmask(SIG_BLOCK,&new_set,&old_set);

    int cnt = 10;

    while(true)
    {
        sigset_t pending;
        sigpending(&pending);
        PrintPending(pending);

        if(cnt == 0)
        {
            sigprocmask(SIG_SETMASK,&old_set,&new_set);
            cout<<"解除对2号信号屏蔽"<<endl;
        }
        cnt--;
        sleep(1);
    }
    return 0;
}